using System;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;
using LS.GameKit;
using LS.Input;
using LS.GameSystem;

namespace LS
{
    public partial class LSEntry : MonoBehaviour
    {
        internal static Dictionary<Type, ComponentReference> s_CustomComponentTypeMap = new Dictionary<Type, ComponentReference>();

        public static BuiltinDataComponent BuiltinData => GetCustomGFComponent<BuiltinDataComponent>();

        public static LSBaseComponent LSBase => GetCustomGFComponent<LSBaseComponent>();

        public static CoroutineComponent Coroutine => GetCustomGFComponent<CoroutineComponent>();

        public static LocalizationLoaderComponent LocalizationLoader => GetCustomGFComponent<LocalizationLoaderComponent>();

        public static SavingComponent Saving => GetCustomGFComponent<SavingComponent>();
        
        public static GameSystemComponent GameSystem => GetCustomGFComponent<GameSystemComponent>();
        
        public static InputComponent Input => GetCustomGFComponent<InputComponent>();

        public static T GetCustomGFComponent<T>()where T: GameFrameworkComponent
        {
            var type = typeof(T);
            return GetCustomGFComponent(type) as T;
        }

        public static GameFrameworkComponent GetCustomGFComponent(System.Type componentType)
        {
            if (s_CustomComponentTypeMap.TryGetValue(componentType, out var componentRef))
            {
                return componentRef.Component;
            }

            var componennt = GameEntry.GetComponent(componentType);
            s_CustomComponentTypeMap.Add(componentType, new ComponentReference(componennt));
            return componennt;
        }

    }
}
